package com.cloudDemoOrder.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudDemoOrder.service.OrderService;
import entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * order控制层
 *
 * @author Daft_V
 * @date 2022/5/6 9:59
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @GetMapping("/orderList")
    @ResponseBody
    public JSONObject orderList() {
        List<Order> orderList = orderService.orderList();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", orderList);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }

    /**
     * 提交订单
     *
     * @param uid    userId
     * @param pid    productId
     * @param number 购买数量
     * @return JSONObject
     */
    @PostMapping("/insertOrder")
    @ResponseBody
    public JSONObject insertOrder(int uid, int pid, int number) {
        JSONObject insertFlag = orderService.insertOrder(uid, pid, number);
        return insertFlag;
    }

}
